package action;

import javax.servlet.http.HttpSession;

import model.GroupInfo;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import util.ResponseUtil;
import dao.ScheduleInfoDao;

public class ScheduleInfoAction extends BaseAction {

	private static final long serialVersionUID = 8107531738436035836L;
	
	private GroupInfo groupInfo;
	
	public GroupInfo getGroupInfo() {
		return groupInfo;
	}

	public void setGroupInfo(GroupInfo groupInfo) {
		this.groupInfo = groupInfo;
	}

	ScheduleInfoDao scheduleInfoDao = new ScheduleInfoDao();
	
	@Override
	public String execute() throws Exception {
		HttpSession session = request.getSession();
		JSONObject result = new JSONObject();
		try {
			scheduleInfoDao.updateToNewTable(session, groupInfo);
			result.put("success", "true");
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}
	
	public void loadProgress() throws Exception {
		HttpSession session = request.getSession();
		JSONObject result = new JSONObject();
		try {
			JSONObject progress = scheduleInfoDao.loadProgress(session, groupInfo);
			result.put("progress", progress.get("PROGRESS"));
			result.put("success", "true");
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
	}
}
